package com.situ.mallsdauweb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.mallsdauweb.entity.Cart;
import com.situ.mallsdauweb.entity.MemberAddress;
import com.situ.mallsdauweb.service.ICartService;
import com.situ.mallsdauweb.service.IMemberAddressService;
import com.situ.mallsdauweb.util.RequestUtil;
import com.situ.mallsdauweb.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author han
 * @since 2023-06-04
 */
@Controller
@RequestMapping("/")
public class CartController {
    @Autowired
    private ICartService cartService;

    @Autowired
    private IMemberAddressService memberAddressService;

    @GetMapping("/cart")
    public String cart(Model model) {
        List<CartVO> list=cartService.cartList();
        model.addAttribute("carts",list);
        return "cart";
    }
    @PostMapping("/cart/add")
    @ResponseBody
    public void addCrat(Integer id){
        cartService.addCart(id);
    }

    @PostMapping("/cart/del")
    @ResponseBody
    public void del(Integer id){
        cartService.removeById(id);
    }

    @PostMapping("/cart/amount")
    @ResponseBody
    public void amount(Integer id,Integer amount){
        Cart c=new Cart();
        c.setId(id);
        c.setAmount(amount);
        cartService.updateById(c);

    }

    @GetMapping("/confirm")
    public String confirm(String cartIds,Model model){
        //当前用户地址列表
        QueryWrapper<MemberAddress> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("member_id", RequestUtil.getCurrent().getId());
        List<MemberAddress> addressList = memberAddressService.list(queryWrapper);
        //带上当前cartIds这些购物车的数据
        List<CartVO> list=cartService.confirmCartList(cartIds);
        model.addAttribute("addressList",addressList);
        model.addAttribute("carts",list);
        return "confirm";
    }

}
